package com.kzd.btgame.jetpack.api

import com.blankj.utilcode.util.GsonUtils
import com.kzd.btgame.app.*
import com.kzd.btgame.entity.*
import com.kzd.btgame.utils.httpSign
import com.kzd.btgame.utils.key1
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.toClass
import rxhttp.toDownload
import rxhttp.wrapper.entity.Progress

/**
 * 获取验证码
 * */
suspend fun userMobileCode(phone: String, type: Int): HttpResponseDataT<String> =
    RxHttp.postForm(USER_MOBILE_CODE)
        .add("phone", phone)
        .add("type", type)
        .toClass<HttpResponseDataT<String>>()
        .await()

/**
 * 获取用户信息
 * */
suspend fun userGetUserInfo(): HttpResponseDataT<UserInfo> =
    RxHttp.postForm(USER_GET_USER_INFO)
        .toClass<HttpResponseDataT<UserInfo>>()
        .await()

/**
 * 用户名注册
 * */
suspend fun userNameRegister(
    userName: String, passwd: String, type: String, invite: String? = null,
    code: String? = null, sessionid: String? = null,
): HttpResponseDataT<AccessUser> {
    if (SPSConstance.isFirstInstall == false) {
        RxHttp.postForm(APP_INSTALL_FINISH)
            .add("device_id", SPSConstance.deviceId ?: "")
            .toClass<HttpResponseData>()
            .await()
    }
    val http = RxHttp.postForm(USER_USERNAME_REG)
        .add("user_name", userName)
        .add("passwd", passwd)
        .add("type", type)
    if (!invite.isNullOrEmpty()) {
        http.add("invite", invite)
    }
    if (!code.isNullOrEmpty()) {
        http.add("code", code)
    }
    if (!sessionid.isNullOrEmpty()) {
        http.add("sessionid", sessionid)
    }
    val deviceId = SPSConstance.deviceId
    if (!deviceId.isNullOrEmpty()) {
        http.add("device_id", deviceId)
    }
    val uidResponse = http.toClass<HttpResponseDataT<String>>().await()
    if (uidResponse.code != 1) {
        throw CustomException(uidResponse.code.toString(), uidResponse.message)
    }
    val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserInfo>>()
        .await()
    val userMineResponse = RxHttp.postForm(USER_MY_INFO)
        .setAssemblyEnabled(false)
        .add("uid", uidResponse.data)
        .toClass<HttpResponseDataT<UserMine>>()
        .await()
    val accessUser = AccessUser(
        uid = uidResponse.data,
        userInfo = userInfoResponse.data,
        userMine = userMineResponse.data
    )
    return HttpResponseDataT(accessUser).apply {
        this.code = uidResponse.code
        message = uidResponse.message
    }
}

/**
 * 修改用户信息
 * */
suspend fun userChangeInfo(
    face: String? = null, address: String? = null,
    pet_name: String? = null, gender: String? = null,
    qq: String? = null, name: String? = null, idcard: String? = null,
): HttpResponseDataT<UserInfo> {
    val rxHttp = RxHttp.postForm(USER_CHANGE_USER_INFO)
        .addAll(httpSign(SPSConstance.uid, key = key1))
    if (!face.isNullOrEmpty())
        rxHttp.add("face", face)
    if (!address.isNullOrEmpty())
        rxHttp.add("address", address)
    if (!pet_name.isNullOrEmpty())
        rxHttp.add("pet_name", pet_name)
    if (!gender.isNullOrEmpty())
        rxHttp.add("gender", gender)
    if (!qq.isNullOrEmpty())
        rxHttp.add("qq", qq)
    if (!name.isNullOrEmpty())
        rxHttp.add("name", name)
    if (!idcard.isNullOrEmpty())
        rxHttp.add("idcard", idcard)
    return rxHttp
        .toClass<HttpResponseDataT<UserInfo>>().await()
}

/**
 * 获取我的页面信息
 * */
suspend fun userMyInfo(): HttpResponseDataT<UserMine> =
    RxHttp.postForm(USER_MY_INFO)
        .toClass<HttpResponseDataT<UserMine>>()
        .await()

/**
 * 我的代金券
 * */
suspend fun userMyCoupon(
    type: String, page: String, limit: String,
): HttpResponseDataE<WelfareCoupon> = RxHttp.postForm(USER_MY_COUPON)
    .add("type", type)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<WelfareCoupon>>()
    .await()

/**
 * 代金券领取
 * */
suspend fun userAddCoupon(cid: String): HttpResponseDataT<WelfareCoupon> =
    RxHttp.postForm(USER_ADD_COUPON)
        .add("id", cid)
        .toClass<HttpResponseDataT<WelfareCoupon>>()
        .await()

/**
 * 我的游戏
 * */
suspend fun userMyGame(): HttpResponseDataE<GameBean> =
    RxHttp.postForm(USER_MY_GAME)
        .toClass<HttpResponseDataE<GameBean>>()
        .await()

/**
 * 游戏反馈类型
 * */
suspend fun userGetFeedbackTypes(): HttpResponseDataE<FeedbackType> =
    RxHttp.postForm(USER_GET_FEEDBACK_TYPES)
        .toClass<HttpResponseDataE<FeedbackType>>()
        .await()

/**
 * 省钱卡数据
 * */
suspend fun userGetSavingInfo(): HttpResponseDataT<MemberMoney> =
    RxHttp.postForm(USER_GET_SAVING_INFO)
        .toClass<HttpResponseDataT<MemberMoney>>()
        .await()

/**
 * 省钱卡订单
 * */
suspend fun userSavingApply(
    type: String, money: String, pay_type: String,
): HttpResponseDataT<String> = RxHttp.postForm(USER_SAVING_APPLY)
    .add("type", type)
    .add("money", money)
    .add("pay_type", pay_type)
    .toClass<HttpResponseDataT<String>>()
    .await()

/**
 * 会员中心数据
 * */
suspend fun userGetMemberInfo(): HttpResponseDataT<MemberCenter> =
    RxHttp.postForm(USER_GET_MEMBER_INFO)
        .toClass<HttpResponseDataT<MemberCenter>>()
        .await()

/**
 * 会员中心订单
 * */
suspend fun userMemberCreateOrder(
    id: String, money: String, pay_type: String,
): HttpResponseDataT<String> = RxHttp.postForm(USER_MEMBER_CREATE_ORDER)
    .add("id", id)
    .add("money", money)
    .add("pay_type", pay_type)
    .toClass<HttpResponseDataT<String>>()
    .await()

/**
 * 平台币金额
 * */
suspend fun userGetCoinInfo(): HttpResponseDataT<PlatformData> =
    RxHttp.postForm(USER_GET_COIN_INFO)
        .toClass<HttpResponseDataT<PlatformData>>()
        .await()

/**
 * 平台币订单
 * */
suspend fun userCoinCreateOrder(
    id: String, money: String, pay_type: String, is_have: Int,
): HttpResponseDataT<String> = RxHttp.postForm(USER_COIN_CREATE_ORDER)
    .add("id", id)
    .add("money", money)
    .add("pay_type", pay_type)
    .add("is_have", is_have)
    .toClass<HttpResponseDataT<String>>()
    .await()

/**
 * 新手任务列表
 * */
suspend fun userGetNewbieTask(): HttpResponseDataT<MoneyTaskNew> =
    RxHttp.postForm(USER_GET_NEWBIE_TASK)
        .toClass<HttpResponseDataT<MoneyTaskNew>>()
        .await()

/**
 * 新手任务领取
 * */
suspend fun userNewbieReceive(type: String): HttpResponseData =
    RxHttp.postForm(USER_XIN_SHOU_RECEIVE)
        .add("type", type)
        .toClass<HttpResponseData>()
        .await()

/**
 * 分享app成功
 * */
suspend fun userAddShare(): HttpResponseData =
    RxHttp.postForm(USER_ADD_SHARE)
        .toClass<HttpResponseData>()
        .await()

/**
 * 日常任务列表
 * */
suspend fun userGetMakeMoney(): HttpResponseDataE<MoneyTaskList> =
    RxHttp.postForm(USER_GET_MAKE_MONEY)
        .toClass<HttpResponseDataE<MoneyTaskList>>()
        .await()
/**
 * 任务列表
 * */
suspend fun userGetTodayTask(): HttpResponseDataE<MoneyTask> =
    RxHttp.postForm(USER_GET_TODAY_TASK)
        .toClass<HttpResponseDataE<MoneyTask>>()
        .await()

/**
 * 任务列表领取
 * */
suspend fun userTodayReceive(type: String): HttpResponseData =
    RxHttp.postForm(USER_TODAY_RECEIVE)
        .add("type", type)
        .toClass<HttpResponseData>()
        .await()

/**
 * 大佬须知-共享列表
 * */
suspend fun userShareOrderList(): HttpResponseDataE<MoneyBossShared> =
    RxHttp.postForm(USER_SHARE_ORDER_LIST)
        .toClass<HttpResponseDataE<MoneyBossShared>>()
        .await()
/**
 * 大佬须知-邀请列表
 * */
suspend fun userInviteNotice(): HttpResponseDataE<MoneyBossInvite> =
    RxHttp.postForm(USER_INVITE_NOTICE)
        .toClass<HttpResponseDataE<MoneyBossInvite>>()
        .await()

/**
 * 我的体验金
 * */
suspend fun userGetExperienceCoin(
    page: String, limit: String
): HttpResponseDataE<MoneyExperience> = RxHttp.postForm(USER_GET_EXPERIENCE_COIN)
    .add("p", page)
    .add("limit", limit)
    .toClass<HttpResponseDataE<MoneyExperience>>()
    .await()

/**
 * 消息中心
 * */
suspend fun userFeedback(feedback: Feedback): HttpResponseData {
    val maps: Map<String, String?> =
        GsonUtils.fromJson<Map<String, String?>>(
            GsonUtils.toJson(feedback),
            Map::class.java
        ).filter { it.value != null }
    return RxHttp.postForm(USER_FEEDBACK)
        .addAll(maps)
        .toClass<HttpResponseData>()
        .await()
}

/**
 * 保存收货地址
 * */
suspend fun userChangeAddress(
    id: String? = null, consignee: String, mobile: String, uaddress: String, moren: String,
): HttpResponseDataE<UserAddress> {
    val http = RxHttp.postForm(USER_CHANGE_ADDRESS)
        .add("consignee", consignee)
        .add("mobile", mobile)
        .add("uaddress", uaddress)
        .add("moren", moren)
    if (!id.isNullOrEmpty()) {
        http.add("id", id)
    }
    return http
        .toClass<HttpResponseDataE<UserAddress>>().await()
}

/**
 * 忘记密码
 * */
suspend fun userForgetPassword(
    phone: String, passwd: String, confirmPasswd: String, code: String, sessionId: String,
): HttpResponseData = RxHttp.postForm(USER_FORGET_PASSWORD)
    .add("phone", phone)
    .add("passwd", passwd)
    .add("confirm_passwd", confirmPasswd)
    .add("code", code)
    .add("sessionid", sessionId)
    .toClass<HttpResponseData>()
    .await()


suspend fun userDownload(
    urlPath: String, destPath: String,
    progress: (suspend (Progress) -> Unit)? = null,
): HttpResponseDataT<String> {
    val path = RxHttp.get(urlPath).toDownload(destPath, true, 5, progress)
        .await()
    return HttpResponseDataT(path).apply {
        this.code = 1
        this.message = "下载成功"
    }
}


/**
 * 合并-（新手任务列表，日常任务列表）
 * */
suspend fun userMoneyData(): HttpResponseDataT<MoneyData> {
    val responseSign = personalGetSignInConfig()
    if (responseSign.code != 1) throw CustomException(
        responseSign.code.toString(), responseSign.message
    )
    val responseNew = userGetNewbieTask()
    if (responseNew.code != 1) throw CustomException(
        responseNew.code.toString(), responseNew.message
    )
    val responseList = userGetMakeMoney()
    if (responseList.code != 1) throw CustomException(
        responseList.code.toString(), responseList.message
    )
    val moneyData = MoneyData(
        sign = responseSign.data, taskNew = responseNew.data, taskDay = responseList.data
    )
    return HttpResponseDataT(moneyData).apply {
        code = responseList.code
        message = responseList.message
    }
}

